package com.atguigu.gmall.order.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.common.util.AuthContextHolder;
import com.atguigu.gmall.order.model.OrderInfo;
import com.atguigu.gmall.order.service.OrderInfoService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author: atguigu
 * @create: 2023-08-10 10:17
 */
@RestController
@RequestMapping("/api/order")
public class OrderController {

    @Autowired
    private OrderInfoService orderInfoService;


    /**
     * 当前用户-汇总订单确认所需要数据
     *
     * @return
     */
    @GetMapping("/auth/trade")
    public Result orderTadeData(HttpServletRequest request) {
        //1.获取当前用户ID
        String userId = AuthContextHolder.getUserId(request);

        //2.调用业务层封装订单渲染需要数据 封装Map
        Map<String, Object> mapResult = orderInfoService.getOrderTadeData(Long.valueOf(userId));
        return Result.ok(mapResult);
    }


    /**
     * 订单提交,跟后续支付业务对接
     *
     * @param orderInfo
     * @param tradeNo   流水号
     * @return
     */
    @ApiOperation("订单提交")
    @PostMapping("/auth/submitOrder")
    public Result submitOrder(HttpServletRequest request, @RequestBody OrderInfo orderInfo, @RequestParam("tradeNo") String tradeNo) {
        //1.获取用户ID
        String userId = AuthContextHolder.getUserId(request);
        //2.封装订单对象中用户ID
        orderInfo.setUserId(Long.valueOf(userId));
        //3.调用业务层提交订单 返回新增后订单ID
        Long orderId = orderInfoService.submitOrder(orderInfo, tradeNo);
        return Result.ok(orderId);
    }

    /**
     * 分页查询当前用户订单
     *
     * @param page
     * @param limit
     * @return
     */
    @ApiOperation("分页查询当前用户订单")
    @GetMapping("/auth/{page}/{limit}")
    public Result getOrderList(HttpServletRequest request, @PathVariable("page") int page, @PathVariable("limit") int limit) {
        //1.获取用户ID
        String userId = AuthContextHolder.getUserId(request);
        //2.调用业务分页查询商品列表
        Page<OrderInfo> infoPage = orderInfoService.getOrderList(userId, page, limit);
        return Result.ok(infoPage);
    }


    /**
     * 根据订单ID查询订单信息(订单明细)
     *
     * @param orderId
     * @return
     */
    @ApiOperation("根据订单ID查询订单信息")
    @GetMapping("/inner/getOrderInfo/{orderId}")
    public OrderInfo getOrderInfo(@PathVariable("orderId") Long orderId) {
        return orderInfoService.getOrderInfo(orderId);
    }


    /**
     * 处理库存系统发送拆单请求
     *
     * @param orderId
     * @param wareSkuMap
     * @return
     */
    @PostMapping("/orderSplit")
    public String orderSplit(@RequestParam("orderId") Long orderId, @RequestParam("wareSkuMap") String wareSkuMap) {
        return orderInfoService.orderSplit(orderId, wareSkuMap);
    }


    /**
     * 提供给秒杀服务调用保存秒杀订单
     *
     * @param orderInfo
     * @return
     */
    @PostMapping("/inner/seckill/submitOrder")
    public Long submitSeckillOrder(@RequestBody OrderInfo orderInfo) {
        return orderInfoService.submitSeckillOrder(orderInfo);
    }
}
